package com.uhu.code;

/**
 * 42.接雨水
 * https://leetcode.cn/problems/trapping-rain-water/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class 接雨水 {

    public static void main(String[] args) {
        Solution solution = new Solution();
//        System.out.println(solution.trap(new int[] {0,1,0,2,1,0,1,3,2,1,2,1}) == 6);
//        System.out.println(solution.trap(new int[] {4,2,0,3,2,5}) == 9);
//        System.out.println(solution.trap(new int[] {2,5}) == 0);
//        System.out.println(solution.trap(new int[] {1}) == 0);
        System.out.println(solution.trap(new int[] {4, 2, 3}));
    }

    static class Solution {
        public int trap(int[] height) {
            int result = 0;
            int i = 0;
            // 遍历数组
            while(i < height.length) {
                int j = i+1;
                int tempCount = 0;
                int tempSize = 0;
                while(j < height.length && height[i] > height[j]) {
                    tempCount = tempCount + height[j];
                    tempSize ++;
                    j++;
                }
                if(j == height.length) {

                }
                if(j < height.length  &&  tempSize > 0) {
                    // 计算面积
                    int tempArea = height[i]*tempSize - tempCount;
                    result = result + tempArea;
                    i = j;
                }else {
                    i++;
                }
            }
            return result;
        }
    }
}
